<template>
  <section class="merchant-account-main">
     <section class="main-search merge">
      <span class="svg-container">
        <svg-icon icon-class="search" />
      </span>
      <el-input v-model.trim="listQuery.merchantName" placeholder="输入商户名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
      <section class="search-btn">
        <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
        <el-button type="primary" @click="handleReset">重置</el-button>
      </section>
    </section>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>平台商城商户管理</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="handleCreate">新建</el-button>
      </section>
    </section>

    <section class="main-table">
      <v-table :list="list" tag="merchantmain" :list-loading="listLoading" :filter-column="filtercolumn">
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-merchantName="small-padding fixed-width">
          <template slot-scope="scope">
              <el-button
              type="text"
              size="mini"
              @click="handlePublish(scope.row)"
              >{{ scope.row.status === "ZT01" ? "禁用" : "启用" }}</el-button
            >
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </v-table>
       <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> 
    </section>
    <el-dialog v-el-drag-dialog :destroy-on-close="true" :visible.sync="visible" :close-on-click-modal="false" title="新建/编辑商城商户" center width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model.trim="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="商户号" prop="merchantNumber">
          <el-input  v-model.trim="form.merchantNumber" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item  label="salt密钥" prop="merchantSalt">
          <el-input  type="textarea" :rows="3" v-model.trim="form.merchantSalt" placeholder="请输入salt密钥" />
        </el-form-item>
      
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="ZT01">启用</el-radio>
            <el-radio label="ZT02">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
				<el-form-item label="备注" prop="remark">
				      <el-input
				        type="textarea"
				        :rows="5"
				        placeholder="请输入备注"
				        v-model="form.remark">
				      </el-input>
				</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" style="padding: 10px 30px" @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="state === 'create'? createSubmit() : updateSubmit()">确 定</el-button>
      </div>
    </el-dialog>

  </section>
</template>
<script>
import { queryPage, insert, update,getDictByCode,get,updateState } from './../api'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
import CryptoJS from '@/utils/aesJiaJM'
export default {
  name: 'merchantAccountMain',
  components: { VTable, Pagination },
	directives: { elDragDialog },
  data() {
 
    return {
      listQuery: {
        page: 1,
        limit: 10,
				merchantName: ''
      },
      list: [],
      total: 0,
      listLoading: false,
			visible: false,
			state: 'create',
			form: {
				id: '',
				merchantName: '',
				merchantNumber: '',
        merchantSalt:'',
        remark:'',
				status: 'ZT01'
			},
			rules: {
				merchantName: [{ required: true, message: '请输入商户名称', trigger: 'blur' }],
				merchantNumber: [{ required: true, message: '请输入商户号', trigger: 'blur' }],
        merchantSalt: [{ required: true,message: '请输入密钥', trigger: 'blur' }],
        // remark: [{ required: true, message: '请选择账户类型', trigger: 'change' }],
				status: [{ required: true, message: '请选择状态', trigger: 'change' }]
			},
    }
  },
  created() {
    this.getList()
  },
  methods: {
     /**
     * 启用/禁用
     */
    handlePublish(row) {
      this.$confirm(row.status === 'ZT01' ? '确认禁用？' : '确认启用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateState({ id: row.id, status: row.status === 'ZT01' ? 'ZT02' : 'ZT01' }).then(res => {
          if (res.data.code == '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          }
        })
      }).catch(() => {

      });
    },
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code).then(response => {
          this.dict[code] = response.data.data[0].list
        }).catch(error => {
          reject(error)
        })
      })
    },
    filtercolumn(scope, item) {
        return scope.row[item]
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.merchantName = ''
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleCreate() {
        console.log('form',this.form)
			for (const key in this.form) {
				if (key === 'status') {
					this.form[key] = 'ZT01'
				} else {
					this.form[key] = ''
				}
			}
			this.state = 'create'
			this.visible = true
    },

    handleUpdate(row) {
      let ths=this
      this.getData(row.id).then(res=>{
        for (const key in ths.form) {
            this.form[key] = res[key]
        }
        ths.state = 'update'
        ths.visible = true
      }).catch(err=>{
        ths.$message.error('获取详情失败')
      })
    },
    getData(id){
      return new Promise((resolve, reject) => {
        get(id).then(res => {
          if(res.data.code=='200'){
            resolve(res.data.data)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
		/**
     * 新增提交
		 */
		createSubmit() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					const loading = this.$loading({
						lock: true,
						text: '请稍后',
						spinner: 'el-icon-loading',
						background: 'rgba(0, 0, 0, 0.3)'
					})
					const params = Object.assign({}, this.form)
					return new Promise((resolve, reject) => {
						insert(params).then(() => {
							this.dialogVisible = false
							loading.close()
							this.$message({
								title: '成功',
								message: '操作成功',
								type: 'success',
								duration: 2000
							})
							this.visible = false
							this.getList()
						}).catch(error => {
							loading.close()
							reject(error)
						})
					})
				} else {
					return false
				}
			})
    },
		/**
     * 编辑提交
		 */
		updateSubmit() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					const loading = this.$loading({
						lock: true,
						text: '请稍后',
						spinner: 'el-icon-loading',
						background: 'rgba(0, 0, 0, 0.3)'
					})
					const params = Object.assign({}, this.form)
					return new Promise((resolve, reject) => {
						update(params).then(() => {
							this.dialogVisible = false
            
							loading.close()
							this.$message({
								title: '成功',
								message: '操作成功',
								type: 'success',
								duration: 2000
							})
							this.visible = false
							this.getList()
						}).catch(error => {
							loading.close()
							reject(error)
						})
					})
				} else {
					return false
				}
			})
    },
    getList(obj) {
			this.listLoading = true
			var query = {
				page: obj ? obj.page : this.listQuery.page,
				pageSize: obj ? obj.limit : this.listQuery.limit,
				merchantName: this.listQuery.merchantName
			}
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
@import '@/styles/mixin.scss';
.merchant-account-main {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-bottom: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
      .num {
        @include font-color($mainColor);
      }
    }
  }
}
</style>
